package net.keebook.webapp.util;

import java.io.IOException;

import net.keebook.webapp.entity.ItemGroup;
import net.keebook.webapp.entity.PasswordItem;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.WorkbookUtil;

public class ExportXls extends DatabaseExport {
	@Override
	public void export() throws IOException {
		Workbook wb = new HSSFWorkbook();
		Sheet sheet = wb.createSheet(WorkbookUtil.createSafeSheetName(getKeeBook().getName()));
		
		Row headlineRow = sheet.createRow((short)0);
		headlineRow.createCell(0).setCellValue("Title");
		headlineRow.createCell(1).setCellValue("Group");
		headlineRow.createCell(2).setCellValue("Username");
		headlineRow.createCell(3).setCellValue("Password");
		headlineRow.createCell(4).setCellValue("URL");
		headlineRow.createCell(5).setCellValue("Notes");
		
		int r = 1;
		for (PasswordItem item : getItems()) {
			Row row = sheet.createRow(r);
			row.createCell(0).setCellValue(item.getTitle());
			row.createCell(1).setCellValue(item.getGroup() instanceof ItemGroup ? item.getGroup().getTitle() : "");
			row.createCell(2).setCellValue(item.getUsername());
			row.createCell(3).setCellValue(item.getPassword());
			row.createCell(4).setCellValue(item.getUrl());
			row.createCell(5).setCellValue(item.getNotes());
			r++;
		}
		
		// Apply style to all cells
		CellStyle style = createDefaultCellStyle(wb);
		CellStyle headlineStyle = createHeadlineCellStyle(wb);
		CellStyle notesStyle = createNotesCellStyle(wb);
		for (int i=0; i<r; i++) {
			Row row = sheet.getRow(i);
			for (int c=0; c<=5; c++) {
				if (i == 0) {
					row.getCell(c).setCellStyle(headlineStyle);
				} else {
					if (c == 5) {
						row.getCell(c).setCellStyle(notesStyle);
					} else {
						row.getCell(c).setCellStyle(style);
					}
				}
			}
		}
		
		// Fit content width
		for (r=0; r<=5; r++) {
			sheet.autoSizeColumn(r);
		}
		
		wb.write(getOutputStream());
	}
	
	private CellStyle createDefaultCellStyle(Workbook wb) {
		CellStyle style = wb.createCellStyle();
	    style.setBorderBottom(CellStyle.BORDER_THIN);
	    style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
	    style.setBorderLeft(CellStyle.BORDER_THIN);
	    style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
	    style.setBorderRight(CellStyle.BORDER_THIN);
	    style.setRightBorderColor(IndexedColors.BLACK.getIndex());
	    style.setBorderTop(CellStyle.BORDER_THIN);
	    style.setTopBorderColor(IndexedColors.BLACK.getIndex());
	    return style;
	}
	
	private CellStyle createHeadlineCellStyle(Workbook wb) {
		CellStyle style = createDefaultCellStyle(wb);
		Font font = wb.createFont();
		font.setBoldweight(Font.BOLDWEIGHT_BOLD);
		style.setFont(font);
	    return style;
	}
	
	private CellStyle createNotesCellStyle(Workbook wb) {
		CellStyle style = createDefaultCellStyle(wb);
		style.setWrapText(true);
	    return style;
	}

	@Override
	public String getFileExtension() {
		return "xls";
	}
}
